package fr.charlott.core.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import fr.charlott.core.repository.ReunionJpaRepository;
import fr.charlott.core.repository.model.Hotesse;
import fr.charlott.core.repository.model.Reunion;
import fr.charlott.core.service.ReunionService;

@Service("coreReunionService")
public class ReunionServiceImpl implements ReunionService {

    private ReunionJpaRepository reunionJpaRepository;

    public ReunionServiceImpl() {
        super();
    }

    @Autowired
    public ReunionServiceImpl(ReunionJpaRepository reunionJpaRepository) {
        this();
        this.reunionJpaRepository = reunionJpaRepository;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Reunion> chargerToutesLesReunions() {
        return reunionJpaRepository.findAll();
    }

    @Override
    @Transactional
    public void ajouterReunion(Reunion reunion) {
        Reunion reunionExist = chercherParDateEtHotesse(reunion.getDate(), reunion.getHotesse());
        if (reunionExist != null) {
            reunionExist.setChiffreAffaire(reunion.getChiffreAffaire());
            reunionJpaRepository.saveAndFlush(reunionExist);
        } else {
            reunionJpaRepository.saveAndFlush(reunion);
        }
    }

    @Override
    @Transactional
    public void supprimerReunion(Reunion reunion) {
        reunionJpaRepository.delete(reunion);
    }

    @Override
    @Transactional
    public void supprimerToutesLesReunions() {
        reunionJpaRepository.deleteAll();
    }

    @Override
    @Transactional(readOnly = true)
    public Reunion chercherParId(Long id) {
        return reunionJpaRepository.findOne(id);
    }

    @Override
    @Transactional(readOnly = true)
    public Reunion chercherParDateEtHotesse(Date date, Hotesse hotesse) {
        return reunionJpaRepository.findByDateAndHotesse(date, hotesse);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Reunion> chercherParHotesse(Hotesse hotesse) {
        return reunionJpaRepository.findByHotesse(hotesse);
    }

    @Override
    @Transactional(readOnly = true)
    public double chargerCAParMois(int mois) {
        // return reunionJpaRepository.loadCAByMonth(mois);
        return 0;
    }

    // getters - setters

    /**
     * @param reunionJpaRepository
     *            the reunionJpaRepository to set
     */
    public void setReunionJpaRepository(ReunionJpaRepository reunionJpaRepository) {
        this.reunionJpaRepository = reunionJpaRepository;
    }

}
